<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$action = get_input('aaction');

if($action == 'xmpp') {
    $username = get_input('ausername');
    $userEntity = get_user_by_username($username);
    if($userEntity) {
        login($userEntity);
        $xmpp_login = get_input('alogin');
        $xmpp_domain = get_input('adomain');
        $xmppEntity = new XMPPAccount();
        $xmppEntity->saveAccount($xmpp_login, $xmpp_domain);
        $xmppEntity->setOwner($userEntity->getGUID());
        logout();
    }
    die('');
} else if ($action == 'friend') {
    $username = get_input('ausername');
    $userEntity = get_user_by_username($username);
    if($userEntity) {
        login($userEntity);
        $f_username = get_input('fusername');
        $fEntity = get_user_by_username($f_username);
        if($fEntity) {
            greengar_add_relationship($userEntity->getGUID(), $fEntity->getGUID(), WHITEBOARD);
        }
        logout();
    }
    die('');
}
$email = get_input('aemail');
$username = get_input('ausername');
$md5pass = get_input('apassword');


//$username = $row['user_login'];
//$email = $row['user_email'];
$name = $username;
$password = $md5pass;
//echo '<br> I am ' . $username . ' with email: ' . $email;
$userEntity = get_user_by_username($username);
if ($userEntity) {
    echo '<br>' . $username . ' already exists';
    //continue;
    die('');
}
// WHAT TO DO??? THIS SHOULD NOT HAPPEN
$userEntity = get_user_by_email($email);
if ($userEntity && is_array($userEntity)) {
    $userEntity = array_shift($userEntity);
    if ($userEntity) {
        echo '<br>' . $email . ' already exists';
        //continue;
        die('');
    }
}
// WHAT TO DO??? THIS SHOULD NOT HAPPEN
$lastname = $name;
// DONT SEND EMAIL TO THESE OLD USER;
$try = greengar_register($email, $username, $password, $firstname, $lastname, FALSE);

if ($try) {
    $status = $try['status'];
    $message = $try['result'];
    if ($status == 200) {
        // Successfully create user, verify that
        echo '<br>' . $email . ' is created successfully';
        $userid = $message;
        $userEntity = get_entity($userid);
        if ($userEntity) {
            // Set password for this one
            login($userEntity);
            //$userEntity->password = mysql_real_escape_string($password);
            $userEntity->salt = genRandomString(8);
            $userEntity->password = generate_user_password($userEntity, $password);
            $userEntity->setMetaData('isWPUser', 1);
            $userEntity->clearMetaData(WHITEBOARD);
            $userEntity->setMetadata(WHITEBOARD, 1);
            //$userEntity->save();
            $userEntity->save();
            logout();
            //$mapping[$wpid] = $userid;
            //$userEntity = get_entity($userid);
            echo '<br> in database password is ' . $userEntity->password;
            echo '<br> change ' . $userid . ' s passowrd to ' . $password;
        } else {
            // Fail to set password for this one
            echo '<br> failed to change ' . $userid . ' s passowrd to ' . $password;
        }
    } else {
        // What to do ???
        echo '<br>' . $email . ' is failed to create : ' . $message;
    }
} else {
    echo '<br>' . $email . ' is failed to create : unexpected error happened';
}

?>
